package com.qf.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class ShiroHashRealm extends AuthorizingRealm {


    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

    /**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        // 从token中获取用户名
        String  username = (String) token.getPrincipal();
//        // 从token中获取密码
//        String  password = (String) token.getCredentials();


        System.err.println("username="+username);
//        System.err.println("password="+password);

        //模拟从数据库获取用户凭证信息(password)和salt
        String password = "a1aaf662e9577b87157f329e1c26eaef";
        String salt = "ak47";
        SimpleAuthenticationInfo simpleAuthenticationInfo =
                new SimpleAuthenticationInfo(username, password, ByteSource.Util.bytes(salt),this.getName());

        return simpleAuthenticationInfo;
    }
}
